{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 12.2 \u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u5df2\u7ecf\u542f\u52a8\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5df2\u7ecf\u542f\u52a8\u4e86\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4f46\u662f\u4f60\u60f3\u77e5\u9053\u5b83\u662f\u4e0d\u662f\u771f\u7684\u5df2\u7ecf\u5f00\u59cb\u8fd0\u884c\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ebf\u7a0b\u7684\u4e00\u4e2a\u5173\u952e\u7279\u6027\u662f\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u662f\u72ec\u7acb\u8fd0\u884c\u4e14\u72b6\u6001\u4e0d\u53ef\u9884\u6d4b\u3002\u5982\u679c\u7a0b\u5e8f\u4e2d\u7684\u5176\u4ed6\u7ebf\u7a0b\u9700\u8981\u901a\u8fc7\u5224\u65ad\u67d0\u4e2a\u7ebf\u7a0b\u7684\u72b6\u6001\u6765\u786e\u5b9a\u81ea\u5df1\u4e0b\u4e00\u6b65\u7684\u64cd\u4f5c\uff0c\u8fd9\u65f6\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898\u5c31\u4f1a\u53d8\u5f97\u975e\u5e38\u68d8\u624b\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 threading \u5e93\u4e2d\u7684 Event \u5bf9\u8c61\u3002\nEvent \u5bf9\u8c61\u5305\u542b\u4e00\u4e2a\u53ef\u7531\u7ebf\u7a0b\u8bbe\u7f6e\u7684\u4fe1\u53f7\u6807\u5fd7\uff0c\u5b83\u5141\u8bb8\u7ebf\u7a0b\u7b49\u5f85\u67d0\u4e9b\u4e8b\u4ef6\u7684\u53d1\u751f\u3002\u5728\u521d\u59cb\u60c5\u51b5\u4e0b\uff0cevent \u5bf9\u8c61\u4e2d\u7684\u4fe1\u53f7\u6807\u5fd7\u88ab\u8bbe\u7f6e\u4e3a\u5047\u3002\u5982\u679c\u6709\u7ebf\u7a0b\u7b49\u5f85\u4e00\u4e2a event \u5bf9\u8c61\uff0c\u800c\u8fd9\u4e2a event \u5bf9\u8c61\u7684\u6807\u5fd7\u4e3a\u5047\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7ebf\u7a0b\u5c06\u4f1a\u88ab\u4e00\u76f4\u963b\u585e\u76f4\u81f3\u8be5\u6807\u5fd7\u4e3a\u771f\u3002\u4e00\u4e2a\u7ebf\u7a0b\u5982\u679c\u5c06\u4e00\u4e2a event \u5bf9\u8c61\u7684\u4fe1\u53f7\u6807\u5fd7\u8bbe\u7f6e\u4e3a\u771f\uff0c\u5b83\u5c06\u5524\u9192\u6240\u6709\u7b49\u5f85\u8fd9\u4e2a event \u5bf9\u8c61\u7684\u7ebf\u7a0b\u3002\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u4e00\u4e2a\u5df2\u7ecf\u88ab\u8bbe\u7f6e\u4e3a\u771f\u7684 event \u5bf9\u8c61\uff0c\u90a3\u4e48\u5b83\u5c06\u5ffd\u7565\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u7ee7\u7eed\u6267\u884c\u3002\n\u4e0b\u8fb9\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Event \u6765\u534f\u8c03\u7ebf\u7a0b\u7684\u542f\u52a8\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from threading import Thread, Event\nimport time\n\n# Code to execute in an independent thread\ndef countdown(n, started_evt):\n    print('countdown starting')\n    started_evt.set()\n    while n > 0:\n        print('T-minus', n)\n        n -= 1\n        time.sleep(5)\n\n# Create the event object that will be used to signal startup\nstarted_evt = Event()\n\n# Launch the thread and pass the startup event\nprint('Launching countdown')\nt = Thread(target=countdown, args=(10,started_evt))\nt.start()\n\n# Wait for the thread to start\nstarted_evt.wait()\nprint('countdown is running')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u6267\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u201ccountdown is running\u201d \u603b\u662f\u663e\u793a\u5728 \u201ccountdown starting\u201d \u4e4b\u540e\u663e\u793a\u3002\u8fd9\u662f\u7531\u4e8e\u4f7f\u7528 event \u6765\u534f\u8c03\u7ebf\u7a0b\uff0c\u4f7f\u5f97\u4e3b\u7ebf\u7a0b\u8981\u7b49\u5230 countdown() \u51fd\u6570\u8f93\u51fa\u542f\u52a8\u4fe1\u606f\u540e\uff0c\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "event \u5bf9\u8c61\u6700\u597d\u5355\u6b21\u4f7f\u7528\uff0c\u5c31\u662f\u8bf4\uff0c\u4f60\u521b\u5efa\u4e00\u4e2a event \u5bf9\u8c61\uff0c\u8ba9\u67d0\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u4e00\u65e6\u8fd9\u4e2a\u5bf9\u8c61\u88ab\u8bbe\u7f6e\u4e3a\u771f\uff0c\u4f60\u5c31\u5e94\u8be5\u4e22\u5f03\u5b83\u3002\u5c3d\u7ba1\u53ef\u4ee5\u901a\u8fc7 clear() \u65b9\u6cd5\u6765\u91cd\u7f6e event \u5bf9\u8c61\uff0c\u4f46\u662f\u5f88\u96be\u786e\u4fdd\u5b89\u5168\u5730\u6e05\u7406 event \u5bf9\u8c61\u5e76\u5bf9\u5b83\u91cd\u65b0\u8d4b\u503c\u3002\u5f88\u53ef\u80fd\u4f1a\u53d1\u751f\u9519\u8fc7\u4e8b\u4ef6\u3001\u6b7b\u9501\u6216\u8005\u5176\u4ed6\u95ee\u9898\uff08\u7279\u522b\u662f\uff0c\u4f60\u65e0\u6cd5\u4fdd\u8bc1\u91cd\u7f6e event \u5bf9\u8c61\u7684\u4ee3\u7801\u4f1a\u5728\u7ebf\u7a0b\u518d\u6b21\u7b49\u5f85\u8fd9\u4e2a event \u5bf9\u8c61\u4e4b\u524d\u6267\u884c\uff09\u3002\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u9700\u8981\u4e0d\u505c\u5730\u91cd\u590d\u4f7f\u7528 event \u5bf9\u8c61\uff0c\u4f60\u6700\u597d\u4f7f\u7528 Condition \u5bf9\u8c61\u6765\u4ee3\u66ff\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u4f7f\u7528 Condition \u5bf9\u8c61\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5468\u671f\u5b9a\u65f6\u5668\uff0c\u6bcf\u5f53\u5b9a\u65f6\u5668\u8d85\u65f6\u7684\u65f6\u5019\uff0c\u5176\u4ed6\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u76d1\u6d4b\u5230\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading\nimport time\n\nclass PeriodicTimer:\n    def __init__(self, interval):\n        self._interval = interval\n        self._flag = 0\n        self._cv = threading.Condition()\n\n    def start(self):\n        t = threading.Thread(target=self.run)\n        t.daemon = True\n\n        t.start()\n\n    def run(self):\n        '''\n        Run the timer and notify waiting threads after each interval\n        '''\n        while True:\n            time.sleep(self._interval)\n            with self._cv:\n                 self._flag ^= 1\n                 self._cv.notify_all()\n\n    def wait_for_tick(self):\n        '''\n        Wait for the next tick of the timer\n        '''\n        with self._cv:\n            last_flag = self._flag\n            while last_flag == self._flag:\n                self._cv.wait()\n\n# Example use of the timer\nptimer = PeriodicTimer(5)\nptimer.start()\n\n# Two threads that synchronize on the timer\ndef countdown(nticks):\n    while nticks > 0:\n        ptimer.wait_for_tick()\n        print('T-minus', nticks)\n        nticks -= 1\n\ndef countup(last):\n    n = 0\n    while n < last:\n        ptimer.wait_for_tick()\n        print('Counting', n)\n        n += 1\n\nthreading.Thread(target=countdown, args=(10,)).start()\nthreading.Thread(target=countup, args=(5,)).start()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "event\u5bf9\u8c61\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u70b9\u662f\u5f53\u5b83\u88ab\u8bbe\u7f6e\u4e3a\u771f\u65f6\u4f1a\u5524\u9192\u6240\u6709\u7b49\u5f85\u5b83\u7684\u7ebf\u7a0b\u3002\u5982\u679c\u4f60\u53ea\u60f3\u5524\u9192\u5355\u4e2a\u7ebf\u7a0b\uff0c\u6700\u597d\u662f\u4f7f\u7528\u4fe1\u53f7\u91cf\u6216\u8005 Condition \u5bf9\u8c61\u6765\u66ff\u4ee3\u3002\u8003\u8651\u4e00\u4e0b\u8fd9\u6bb5\u4f7f\u7528\u4fe1\u53f7\u91cf\u5b9e\u73b0\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Worker thread\ndef worker(n, sema):\n    # Wait to be signaled\n    sema.acquire()\n\n    # Do some work\n    print('Working', n)\n\n# Create some threads\nsema = threading.Semaphore(0)\nnworkers = 10\nfor n in range(nworkers):\n    t = threading.Thread(target=worker, args=(n, sema,))\n    t.start()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd0\u884c\u4e0a\u8fb9\u7684\u4ee3\u7801\u5c06\u4f1a\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u4ec0\u4e48\u4e8b\u60c5\u53d1\u751f\u3002\u8fd9\u662f\u56e0\u4e3a\u6240\u6709\u7684\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\u83b7\u53d6\u4fe1\u53f7\u91cf\u3002\u6bcf\u6b21\u4fe1\u53f7\u91cf\u88ab\u91ca\u653e\uff0c\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u4f1a\u88ab\u5524\u9192\u5e76\u6267\u884c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sema.release()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sema.release()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7f16\u5199\u6d89\u53ca\u5230\u5927\u91cf\u7684\u7ebf\u7a0b\u95f4\u540c\u6b65\u95ee\u9898\u7684\u4ee3\u7801\u4f1a\u8ba9\u4f60\u75db\u4e0d\u6b32\u751f\u3002\u6bd4\u8f83\u5408\u9002\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u961f\u5217\u6765\u8fdb\u884c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u6216\u8005\u6bcf\u4e2a\u628a\u7ebf\u7a0b\u5f53\u4f5c\u4e00\u4e2aActor\uff0c\u5229\u7528Actor\u6a21\u578b\u6765\u63a7\u5236\u5e76\u53d1\u3002\u4e0b\u4e00\u8282\u5c06\u4f1a\u4ecb\u7ecd\u5230\u961f\u5217\uff0c\u800cActor\u6a21\u578b\u5c06\u572812.10\u8282\u4ecb\u7ecd\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}